// import { excelExportProg, excelImportProg } from "../api/sysconfig"
import { TeListener } from "./eListener"
class ViewFun {
    onRouteAfter = new TeListener<any>()

    routeDtMap = new Map<string, any>()

    private callTableHeight_fun?: (h: number) => void
    private callWindowResize_fun?: () => void

    expData = {
        doing: false,
        prog: 0,
        callProg: undefined as any as (_v: number) => void
    }
    impData = {
        doing: false,
        prog: 0,
        callProg: undefined as any as (_v: number) => void
    }

    msgData = {
        doing: false,
        callDo: undefined as any as () => void,
        callDoOnce: (call: () => void) => {
            this.msgData.callDo = call
            this.msgData.doing = true
            call()
        }
    }

    pageDt: any = {}

    constructor() {
        this._init()
        this._initCheck()
        this._initMsg()
    }

    private _init() {
        window.addEventListener('resize', this.doResize.bind(this))
    }

    private async _initCheck() {
        // const scope = this, ed = scope.expData, imd = scope.impData
        while (true) {
            await window.awaitTime(999)
            // if (imd.doing) scope.doExcelImportProg()
            // if (ed.doing) scope.doExcelExportProg()
        }
    }


    private async _initMsg() {
        const scope = this, d = scope.msgData
        while (true) {
            await window.awaitTime(15000)
            if (d.doing) d.callDo?.()
        }
    }

    // private async doExcelImportProg() {
    //     const imd = this.impData
    //     const rd = await excelImportProg()
    //     if (rd?.data?.code === 0) {
    //         const prog = Number(rd.data.tdata?.percent) || 0
    //         if (prog >= 100) imd.doing = false
    //         imd.prog = prog
    //         imd.callProg?.(prog)
    //     }
    // }
    // private async doExcelExportProg() {
    //     const ed = this.expData
    //     const rd = await excelExportProg()
    //     if (rd?.data?.code === 0) {
    //         const prog = Number(rd.data.tdata?.percent) || 0
    //         if (prog >= 100) ed.doing = false
    //         ed.prog = prog
    //         ed.callProg?.(prog)
    //     }
    // }

    doResize() {
        if (this.callTableHeight_fun) {
            const hsEl = document.getElementById('header-search') as HTMLElement
            const hsh = hsEl.clientHeight || 80
            const h = Math.max(document.documentElement.clientHeight - 406 - hsh, 100)
            this.callTableHeight_fun(h)
        }
        this.callWindowResize_fun?.()
    }

    callWindowResoze(call?: () => void) {
        this.callWindowResize_fun = call
    }

    callTableHeight(call?: (h: number) => void) {
        this.callTableHeight_fun = call
        this.doResize()
    }

    doUpVal_class(obj: any) {
        for (const key in obj) {
            if (obj[key] === '— —') obj[key] = undefined
            // switch (typeof (objVal)) {
            //     case 'string':
            //         if (!objVal) obj[key] = '— —'
            //         break
            // }
        }
    }

    doVal_class(obj: any, ex: string[] = []) {
        for (const key in obj) {
            const objVal = obj[key]
            if (objVal) continue
            if (objVal === 0 || objVal === false) continue
            if (ex.includes(key)) continue
            obj[key] = '— —'
            // switch (typeof (objVal)) {
            //     case 'string':
            //         if (!objVal) obj[key] = '— —'
            //         break
            // }
        }
    }

    doListVal_class(objs: any[], callBefore?: (obj: any) => void, callAfter?: (obj: any) => void, ex: string[] = []) {
        for (const obj of objs) {
            callBefore?.(obj)
            this.doVal_class(obj, ex)
            callAfter?.(obj)
        }
    }

    tableScrollTop(te: any, index: number) {
        const targetTop = te.$el.querySelectorAll('.el-table__body tr')?.[index]?.getBoundingClientRect().top || 0
        const containerTop = te.$el.querySelector('.el-table__body')?.getBoundingClientRect().top || 0
        te?.setScrollTop(targetTop - containerTop)
    }

    getNumSt(v: string) {
        const st = (v.match(/\d/g) || [])[0] || '';
        const si = st ? v.indexOf(st) : 0
        return v.slice(0, si)
    }
    getNum_s(v: string) {
        const st = v.match(/\d/g) || [];
        if (st.length > 1) return v.slice(v.indexOf(st[0] as string), v.lastIndexOf(st[st.length - 1]) + 1)
        else if (st.length > 0) return st[0]
        else return ''
    }
    getNumEd(v: string) {
        const st = v.match(/\d/g) || [];
        if (st.length > 0) return v.slice(v.lastIndexOf(st[st.length - 1]) + 1, v.length)
        else return ''
    }
}

export const viewFun = new ViewFun()
